using System.Collections.Generic;


namespace NodeBasedSkillEditor.Model.Node.RT
{
    public class Node_CacheList_RT : RT_Node
    {
        public RT_SingleValuePort RecacheEveryFrame_RT;

        public RT_ValueListPort ValueList_RT;

        public RT_ValueListPort CachedList_RT;

        public Node_CacheList_RT(Node_CacheList sourceData, RT_DAG parentDag) : base(sourceData, parentDag)
        {
            RecacheEveryFrame_RT = new RT_SingleValuePort(EPortIOType.Input, sourceData.RecacheEveryFrame, this);

            ValueList_RT = new RT_ValueListPort(EPortIOType.Input, sourceData.ValueList, this);

            CachedList_RT = new RT_ValueListPort(EPortIOType.Output, sourceData.CachedList, this);
            CachedList_RT.OnGetValueList = GetCached;
        }

        public List<AValue> GetCached(DAG_Executor executor)
        {
            var info = executor.GetExeInfoOfNode(this) as CacheList_Info;
            if (info == null)
            {
                var valueList = ValueList_RT.GetValueList(executor);
                info = new CacheList_Info(valueList.Count);
                info.FrameMark = executor.CurrentFrame;
                foreach (var v in valueList)
                {
                    info.ValueList.Add(v);
                }
                executor.SetExeInfoOfNode(this, info);
            }
            else
            {
                bool recacheEveryFrame = RecacheEveryFrame_RT.GetValue(executor).ToBoolean().Item2;
                if (recacheEveryFrame && info.FrameMark != executor.CurrentFrame)
                {
                    info.FrameMark = executor.CurrentFrame;
                    var valueList = ValueList_RT.GetValueList(executor);
                    info.ValueList.Clear();
                    info.ValueList.Capacity = valueList.Count;
                    foreach (var v in valueList)
                    {
                        info.ValueList.Add(v);
                    }
                }
            }
            return info.ValueList;
        }
    }

    public class CacheList_Info : RT_NodeExeInfo
    {
        public int FrameMark;
        public List<AValue> ValueList;

        public CacheList_Info(int count)
        {
            ValueList = new List<AValue>(count);
        }
    }
}